package com.fengpb.friday.conductor.client.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fengpb.friday.conductor.common.remote.RemoteClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("remote")
public class RemoteController {

    private final RemoteClient remoteClient;

    public RemoteController(RemoteClient remoteClient) {
        this.remoteClient = remoteClient;
    }

    @RequestMapping("/get/{serviceName}")
    public Map<String, Object> get(@PathVariable String serviceName, @RequestBody Map<String, Object> input) {
        String response = remoteClient.get(serviceName, (String) input.get("path"));
        return JSON.parseObject(response, new TypeReference<Map<String, Object>>(){});
    }
}
